using Microsoft.Phone.Controls.LocalizedResources;
using Microsoft.Phone.Controls.Properties;
using System;
using System.Globalization;
using System.Windows.Data;
namespace Microsoft.Phone.Controls
{
	public class RelativeTimeConverter : IValueConverter
	{
		private const double Minute = 60.0;
		private const double Hour = 3600.0;
		private const double Day = 86400.0;
		private const double Week = 604800.0;
		private const double Month = 2635200.0;
		private const double Year = 31536000.0;
		private const string DefaultCulture = "en-US";
		private string[] PluralHourStrings;
		private string[] PluralMinuteStrings;
		private string[] PluralSecondStrings;
		private void SetLocalizationCulture(CultureInfo culture)
		{
			if (!culture.get_Name().Equals("en-US", 4))
			{
				ControlResources.Culture = culture;
			}
			this.PluralHourStrings = new string[]
			{
				ControlResources.XHoursAgo_2To4,
				ControlResources.XHoursAgo_EndsIn1Not11,
				ControlResources.XHoursAgo_EndsIn2To4Not12To14,
				ControlResources.XHoursAgo_Other
			};
			this.PluralMinuteStrings = new string[]
			{
				ControlResources.XMinutesAgo_2To4,
				ControlResources.XMinutesAgo_EndsIn1Not11,
				ControlResources.XMinutesAgo_EndsIn2To4Not12To14,
				ControlResources.XMinutesAgo_Other
			};
			this.PluralSecondStrings = new string[]
			{
				ControlResources.XSecondsAgo_2To4,
				ControlResources.XSecondsAgo_EndsIn1Not11,
				ControlResources.XSecondsAgo_EndsIn2To4Not12To14,
				ControlResources.XSecondsAgo_Other
			};
		}
		private static string GetPluralMonth(int month)
		{
			if (month >= 2 && month <= 4)
			{
				return string.Format(CultureInfo.get_CurrentUICulture(), ControlResources.XMonthsAgo_2To4, new object[]
				{
					month.ToString(ControlResources.Culture)
				});
			}
			if (month < 5 || month > 12)
			{
				throw new ArgumentException(Resources.InvalidNumberOfMonths);
			}
			return string.Format(CultureInfo.get_CurrentUICulture(), ControlResources.XMonthsAgo_5To12, new object[]
			{
				month.ToString(ControlResources.Culture)
			});
		}
		private static string GetPluralTimeUnits(int units, string[] resources)
		{
			int num = units % 10;
			int num2 = units % 100;
			if (units <= 1)
			{
				throw new ArgumentException(Resources.InvalidNumberOfTimeUnits);
			}
			if (units >= 2 && units <= 4)
			{
				return string.Format(CultureInfo.get_CurrentUICulture(), resources[0], new object[]
				{
					units.ToString(ControlResources.Culture)
				});
			}
			if (num == 1 && num2 != 11)
			{
				return string.Format(CultureInfo.get_CurrentUICulture(), resources[1], new object[]
				{
					units.ToString(ControlResources.Culture)
				});
			}
			if (num >= 2 && num <= 4 && (num2 < 12 || num2 > 14))
			{
				return string.Format(CultureInfo.get_CurrentUICulture(), resources[2], new object[]
				{
					units.ToString(ControlResources.Culture)
				});
			}
			return string.Format(CultureInfo.get_CurrentUICulture(), resources[3], new object[]
			{
				units.ToString(ControlResources.Culture)
			});
		}
		private static string GetDayOfWeek(DayOfWeek dow)
		{
			string result;
			switch (dow)
			{
			case 0:
				result = ControlResources.Sunday;
				break;
			case 1:
				result = ControlResources.Monday;
				break;
			case 2:
				result = ControlResources.Tuesday;
				break;
			case 3:
				result = ControlResources.Wednesday;
				break;
			case 4:
				result = ControlResources.Thursday;
				break;
			case 5:
				result = ControlResources.Friday;
				break;
			case 6:
				result = ControlResources.Saturday;
				break;
			default:
				result = ControlResources.Sunday;
				break;
			}
			return result;
		}
		private static string GetOnDayOfWeek(DayOfWeek dow)
		{
			if (dow == 2)
			{
				return string.Format(CultureInfo.get_CurrentUICulture(), ControlResources.OnDayOfWeek_Tuesday, new object[]
				{
					RelativeTimeConverter.GetDayOfWeek(dow)
				});
			}
			return string.Format(CultureInfo.get_CurrentUICulture(), ControlResources.OnDayOfWeek_Other, new object[]
			{
				RelativeTimeConverter.GetDayOfWeek(dow)
			});
		}
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			if (!(value is DateTime))
			{
				throw new ArgumentException(Resources.InvalidDateTimeArgument);
			}
			DateTime dateTime = ((DateTime)value).ToLocalTime();
			DateTime now = DateTime.get_Now();
			TimeSpan timeSpan = now - dateTime;
			this.SetLocalizationCulture(culture);
			string result;
			if (DateTimeFormatHelper.IsFutureDateTime(now, dateTime))
			{
				result = RelativeTimeConverter.GetPluralTimeUnits(2, this.PluralSecondStrings);
			}
			if (timeSpan.get_TotalSeconds() > 31536000.0)
			{
				result = ControlResources.OverAYearAgo;
			}
			else
			{
				if (timeSpan.get_TotalSeconds() > 3952800.0)
				{
					int month = (int)((timeSpan.get_TotalSeconds() + 1317600.0) / 2635200.0);
					result = RelativeTimeConverter.GetPluralMonth(month);
				}
				else
				{
					if (timeSpan.get_TotalSeconds() >= 2116800.0)
					{
						result = ControlResources.AboutAMonthAgo;
					}
					else
					{
						if (timeSpan.get_TotalSeconds() >= 604800.0)
						{
							int num = (int)(timeSpan.get_TotalSeconds() / 604800.0);
							if (num > 1)
							{
								result = string.Format(CultureInfo.get_CurrentUICulture(), ControlResources.XWeeksAgo_2To4, new object[]
								{
									num.ToString(ControlResources.Culture)
								});
							}
							else
							{
								result = ControlResources.AboutAWeekAgo;
							}
						}
						else
						{
							if (timeSpan.get_TotalSeconds() >= 432000.0)
							{
								result = string.Format(CultureInfo.get_CurrentUICulture(), ControlResources.LastDayOfWeek, new object[]
								{
									RelativeTimeConverter.GetDayOfWeek(dateTime.get_DayOfWeek())
								});
							}
							else
							{
								if (timeSpan.get_TotalSeconds() >= 86400.0)
								{
									result = RelativeTimeConverter.GetOnDayOfWeek(dateTime.get_DayOfWeek());
								}
								else
								{
									if (timeSpan.get_TotalSeconds() >= 7200.0)
									{
										int units = (int)(timeSpan.get_TotalSeconds() / 3600.0);
										result = RelativeTimeConverter.GetPluralTimeUnits(units, this.PluralHourStrings);
									}
									else
									{
										if (timeSpan.get_TotalSeconds() >= 3600.0)
										{
											result = ControlResources.AboutAnHourAgo;
										}
										else
										{
											if (timeSpan.get_TotalSeconds() >= 120.0)
											{
												int units2 = (int)(timeSpan.get_TotalSeconds() / 60.0);
												result = RelativeTimeConverter.GetPluralTimeUnits(units2, this.PluralMinuteStrings);
											}
											else
											{
												if (timeSpan.get_TotalSeconds() >= 60.0)
												{
													result = ControlResources.AboutAMinuteAgo;
												}
												else
												{
													int units3 = ((double)((int)timeSpan.get_TotalSeconds()) > 1.0) ? ((int)timeSpan.get_TotalSeconds()) : 2;
													result = RelativeTimeConverter.GetPluralTimeUnits(units3, this.PluralSecondStrings);
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			return result;
		}
		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			throw new NotImplementedException();
		}
	}
}
